# -*- coding: utf-8 -*-
import wx
import SelectArea as SA
import ControlArea as CA

# 主窗口
class MainWindow(wx.Frame):
    selection = None
    img = None
    shotArea = None

    def __init__(self, parent, title):
        # 视窗大小
        wx.Frame.__init__(self, parent, title = title, 
            size = (300, 400)           # 视窗大小
        )
        # 初始化截图控制器
        self.shotArea = CA.ControlArea(self)
        # 设置文本框（预留位置）
        self.control = wx.StaticText(self, style = wx.TE_MULTILINE)
        # 创建位于窗口的底部的状态栏
        self.CreateStatusBar()
        # 设置sizers
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        menuText = [u"开始截图", u"设置区域", u"关闭区域"]
        buttons = []
        for i in range(0, len(menuText)):
            buttons.append(wx.Button(self, -1, menuText[i]))
            self.sizer2.Add(buttons[i], 1, wx.SHAPED)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.sizer2, 1, wx.GROW)
        self.sizer.Add(self.control, 4, wx.EXPAND)       
        # 激活sizer
        self.SetSizer(self.sizer)
        self.SetAutoLayout(True)
        # 注册事件
        self.Bind(wx.EVT_BUTTON, self.OnStart, buttons[0])
        self.Bind(wx.EVT_BUTTON, self.OnSet, buttons[1])
        self.Bind(wx.EVT_BUTTON, self.OnExit, buttons[2])
        # 热键注册
        self.hotkey = wx.NewIdRef()                                 # 创建id
        self.RegisterHotKey(self.hotkey, wx.MOD_CONTROL, ord('Q'))  # 注册热键（按ctrl+Q键响应）
        self.Bind(wx.EVT_HOTKEY, self.OnStart, id=self.hotkey)      # 绑定热键事件 
        # 窗口居中
        self.Center()
        # 显示视窗
        self.Show(True)

    # 开始截图
    def OnStart(self, event):
        self.img = self.shotArea.getPhoto()

    # 关闭截图区域窗口
    def OnExit(self, event):
        self.shotArea.cancelArea()

    # 设置截图窗口
    def OnSet(self, event):
        self.selection = SA.SelectArea(self, self.shotArea)